package com.itheima.service.impl;

import com.itheima.anno.MyBj;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Primary
public class DeptServiceImpl2 implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpService empService;

    @Autowired
    private DeptLogService deptLogService;
    @Override
    @MyBj
    public List<Dept> setAll() {
        List<Dept> depts = deptMapper.setAll();
        return depts;
    }
//删除部门的同时删除这个部门下的所有员工
    @Override
    @Transactional(rollbackFor = ParseException.class)
    @MyBj
    public int delById(Integer deptid) throws ParseException {
        int i = deptMapper.delById(deptid);
        int i2 =  empService.delByDeptId(deptid);
        deptLogService.insert(new DeptLog(null,LocalDateTime.now(),i+""+i2));
//        int a =1/0;//出现运行时异常，spring会自动回滚
        //默认情况下 出现受检异常 不会回滚
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("asfsdgsd342asfasdaw");


        return i;
    }

    @Override
    public int insertDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept);
    }

}
